home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / chasm4.zip / COM2DATA.ASM < prev    next >
Assembly Source File  |  1984-05-01  |  11KB  |  284 lines

  1. ;======================================================
  2. ; COM2DATA  release 2.0
  3. ; (c) 1984 by David Whitman
  4. ;
  5. ; High speed DOS 2.0 filter version
  6. ; of file conversion utility.
  7. ;
  8. ; Reads a COM file from STDIN and
  9. ; writes BASIC data statements to STDOUT
  10. ;
  11. ; Syntax:  COM2DATA [<filename] [>filename] [linenumber]
  12. ;
  13. ; The starting line number defaults to 1000 unless a number
  14. ; is given on the command line.
  15. ;
  16. ; Requires DOS 2.0, will abort under earlier versions.
  17. ;
  18. ; This source file is in CHASM assembler syntax.
  19. ;======================================================
  20.  
  21.  
  22. @dosver        equ     30H             ;get dos version #
  23. @prnchr        equ     02H             ;print character
  24. @prnstr        equ     09H             ;print string
  25. @read          equ     3FH             ;read device
  26. @write         equ     40H             ;write device
  27.  
  28. beep           equ     07H             ;bell character
  29. lf             equ     0AH             ;line feed character
  30. cr             equ     0DH             ;carrage return character
  31. comma          equ     2CH             ;comma character
  32. param_count    equ     [80H]           ;number of chars in param_area
  33. param_area     equ     [81H]           ;command line parameter text
  34. stdin          equ     0000H           ;standard input device
  35. stdout         equ     0001H           ;standard output device
  36. stderror       equ     0002H           ;standard error output device
  37. buf_length     equ     512             ;input buffer size
  38.  
  39. com2data       proc    near
  40.                call    init            ;check dos, print title
  41.                call    get_linenum     ;get starting line number
  42.                call    doit            ;run conversion
  43.                call    cleanup         ;final processing
  44.                int     20H             ;return to dos
  45.                endp
  46.  
  47. init           proc    near            ;initialization routine
  48.                mov     ah, @dosver     ;what dos are we under?
  49.                int     21H
  50.                cmp     al,2            ;dos 2.0 or over?
  51.                jae     a1              ;yes, skip
  52.  
  53.                mov     ah, @prnstr     ;no, bitch
  54.                mov     dx, offset(baddos)
  55.                int     21H
  56.                pop     ax              ;reset stack
  57.                int     20              ;and exit
  58.  
  59. a1             mov     ah, @write      ;send title message
  60.                mov     bx, stderror    ;to stderror
  61.                mov     cx, length(title_msg)
  62.                mov     dx, offset(title_msg_txt)
  63.                int     21H
  64.                ret
  65.  
  66. baddos         db      beep cr lf
  67.                db      'This program requires DOS 2.0!' cr lf
  68.                db      cr lf '$'
  69.  
  70.  
  71. title_msg      count
  72. title_msg_txt  db      cr lf
  73.                db      'COM2DATA version 2.0' cr lf
  74.                db      'Copyright (c) 1984 by D. Whitman' cr lf
  75.                db      cr lf
  76.                endc
  77.                endp
  78.  
  79. get_linenum    proc     near           ;parse command line for
  80.                                        ;starting line number
  81.                xor     ch,ch           ;cx <== # of param chars
  82.                mov     cl, param_count ;   "
  83.                cmp     cl, 0           ;any parameters?
  84.                je      b1              ;no, exit with default
  85.  
  86.                mov     di, offset(param_area)
  87.                mov     al, ' '         ;search for first non-blank
  88.                rep
  89.                scasb
  90.                jcxz    b1              ;nothing? exit with default
  91.  
  92.                dec     di              ;back up to character found
  93.                inc     cx              ; "
  94.  
  95.                xor     ax,ax           ;will hold building linenum
  96.                jmps    enter_convert   ;convert string to binary
  97.  
  98. convert        mov     bx, 10          ;multiply running total by 10
  99.                mul     ax,bx
  100.                jo      bad_num         ;overflow?  error exit
  101. enter_convert  xor     bx,bx           ;clear out top half
  102.                mov     bl, [di]        ;get a digit into al
  103.                inc     di              ;bump pointer
  104.                cmp     bl, '0'         ;must be between 0
  105.                jb      bad_num
  106.                cmp     bl, '9'         ;and 9
  107.                ja      bad_num
  108.                sub     bl, '0'         ;convert to binary
  109.                add     ax, bx          ;add to running total
  110.                jo      bad_num         ;overflow? error exit
  111.                loop    convert
  112.  
  113.                mov     linenum, ax     ;store converted number
  114.                ret                     ;normal return
  115.  
  116. bad_num        mov     ah, @write      ;print error message
  117.                mov     bx, 2           ;on stderror
  118.                mov     cx, length(num_msg)
  119.                mov     dx, offset(num_msg_txt)
  120.                int     21H             ;and use default
  121. b1             ret                     ;normal return
  122.  
  123. linenum        dw      1000            ;line number defaults to 1000
  124.  
  125.  
  126. num_msg        count
  127. num_msg_txt    db      beep cr lf
  128.                db      'Invalid starting line number - Defaulting to 1000' cr lf
  129.                db      cr lf
  130.                endc
  131.                endp
  132.  
  133. doit           proc    near            ;convert infile to data
  134.  
  135. do1            mov     ah, @read       ;read
  136.                mov     bx, stdin       ;from stdin
  137.                mov     cx, buf_length  ;one buffer's worth
  138.                mov     dx, offset(in_buf)
  139.                int     21H
  140.                test    ax, ax          ;test for EOF
  141.                jz      done            ;no bytes? done
  142.  
  143.                mov     cx, ax          ;cx <== # of bytes read
  144.                mov     si, offset(in_buf)
  145. do2            lodsb                   ;
  146.                call    output          ;convert and send to stdout
  147.                loop    do2             ;loop for # of bytes read
  148.                jmps    do1             ;then refill buffer
  149.  
  150. done           ret
  151.                endp
  152.  
  153. output         proc    near            ;convert byte in al to hex string
  154.                                        ;and send to stdout
  155.  
  156.                cmpw    cur_pos, offset(eol) ;is the line full?
  157.                jb      o1              ;no, skip
  158.                call    newline         ;yes, dump buffer
  159.  
  160. o1             call    send_byte       ;print hex value of byte
  161.                addw    cur_pos, 8      ;bump line positions used
  162.                ret                     ;and exit
  163.  
  164. cur_pos        dw      offset(first_hex) ;current position in line
  165.                endp
  166.  
  167.  
  168. newline        proc    near            ;starts a new data line
  169.  
  170.                push    ax              ;save state
  171.                push    bx              ; "
  172.                push    cx              ; "
  173.                push    dx              ; "
  174.                push    di              ; "
  175.  
  176.                mov     al, ' '         ;blank out old number
  177.                mov     di, offset(out_buf)
  178.                mov     cx, 5
  179.                rep
  180.                stosb
  181.  
  182.                mov    ax, linenum      ;print line number
  183.  
  184.                                        ;the following code fragment was written
  185.                                        ;by Bob Smith and published in PC Age
  186.                                        ;Volume 3.1 (Jan. '84) p. 116
  187.  
  188.                mov     bx, 10          ;set up divisor
  189.                xor     cx, cx          ;clear counter
  190. nxt_in         xor     dx, dx          ;clear for division
  191.                div     bx              ;dl <== AX mod 10
  192.                or      dl, '0'         ;convert to ascii digit
  193.                push    dx              ;save digit
  194.                inc     cx              ;bump counter
  195.                and     ax, ax          ;are we done?
  196.                jnz     nxt_in          ;nope, keep going
  197.                                        ;stack now has digits of number
  198.                                        ;end of Bob Smith's code
  199.  
  200.                mov     di, offset(out_buf) ;peel digits off stack
  201. nxt_out        pop     ax